package com.tsing.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsing.wms.config.RedisManager;
import com.tsing.wms.constant.AuthConsts;
import com.tsing.wms.entity.RegionEntity;
import com.tsing.wms.entity.SupplierEntity;
import com.tsing.wms.exception.CommonException;
import com.tsing.wms.exception.ServerException;
import com.tsing.wms.mapper.RegionMapper;
import com.tsing.wms.mapper.SupplierMapper;
import com.tsing.wms.service.IRegionService;
import com.tsing.wms.util.GetLoginUserUtil;
import com.tsing.wms.vo.PageResultVO;
import com.tsing.wms.vo.req.IdReqVO;
import com.tsing.wms.vo.req.IdsReqVO;
import com.tsing.wms.vo.req.RegionPageReqVO;
import com.tsing.wms.vo.req.RegionReqVO;
import com.tsing.wms.vo.resp.RegionRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 供应商地区表 服务实现类
 * </p>
 *
 * @author TheTsing
 * @since 2022-04-13
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, RegionEntity> implements IRegionService {

    @Autowired
    private RedisManager redisManager;

    @Override
    public void addRegion(RegionReqVO reqVO, HttpServletRequest request) {
        if (GetLoginUserUtil.getLoginUserInfo(request.getHeader(AuthConsts.AUTHENTICATION_HEAD), redisManager).getIdentity() != 1) {
            throw new ServerException(CommonException.Proxy.AUTHORIZATION_EXCEPTION);
        }
        if (ObjectUtil.isNotNull(new LambdaQueryChainWrapper<>(baseMapper)
                .select(RegionEntity::getId)
                .eq(RegionEntity::getAddress, reqVO.getAddress())
                .one())) {
            throw new ServerException(CommonException.Proxy.REGION_IS_NOTNULL);
        }
        RegionEntity regionEntity = new RegionEntity();
        BeanUtil.copyProperties(reqVO, regionEntity);
        baseMapper.insert(regionEntity);
    }

    @Resource
    private SupplierMapper supplierMapper;

    @Override
    public void deleteRegion(IdsReqVO reqVO, HttpServletRequest request) {
        if (GetLoginUserUtil.getLoginUserInfo(request.getHeader(AuthConsts.AUTHENTICATION_HEAD), redisManager).getIdentity() != 1) {
            throw new ServerException(CommonException.Proxy.AUTHORIZATION_EXCEPTION);
        }
        for (String id : reqVO.getIds()) {
            if (0 != new LambdaQueryChainWrapper<>(supplierMapper)
                    .select(SupplierEntity::getId)
                    .eq(SupplierEntity::getRegionId, id)
                    .count()) {
                throw new ServerException(CommonException.Proxy.REGION_RELEVANCE_SUPPLIER);
            }
        }
        baseMapper.deleteBatchIds(reqVO.getIds());
    }

    @Override
    public RegionRespVO getRegion(IdReqVO reqVO) {
        RegionEntity regionEntity = baseMapper.selectById(reqVO.getId());
        RegionRespVO regionRespVO = new RegionRespVO();
        BeanUtil.copyProperties(regionEntity, regionRespVO);
        return regionRespVO;
    }

    @Override
    public void updateRegion(RegionReqVO reqVO, HttpServletRequest request) {
        if (GetLoginUserUtil.getLoginUserInfo(request.getHeader(AuthConsts.AUTHENTICATION_HEAD), redisManager).getIdentity() != 1) {
            throw new ServerException(CommonException.Proxy.AUTHORIZATION_EXCEPTION);
        }
        if(StrUtil.isNotBlank(reqVO.getAddress())){
            if (ObjectUtil.isNotNull(new LambdaQueryChainWrapper<>(baseMapper)
                    .select(RegionEntity::getId)
                    .eq(RegionEntity::getAddress, reqVO.getAddress())
                    .ne(RegionEntity::getId, reqVO.getId())
                    .one())) {
                throw new ServerException(CommonException.Proxy.REGION_IS_NOTNULL);
            }
        }
        RegionEntity regionEntity = new RegionEntity();
        BeanUtil.copyProperties(reqVO, regionEntity);
        baseMapper.updateById(regionEntity);
    }

    @Override
    public PageResultVO<RegionRespVO> regionPage(RegionPageReqVO reqVO) {
        IPage<RegionEntity> page = new Page<>(reqVO.getPageNum(), reqVO.getPageSize());
        new LambdaQueryChainWrapper<>(baseMapper)
                .like(StrUtil.isNotBlank(reqVO.getAddress()), RegionEntity::getAddress, reqVO.getAddress())
                .page(page);
        List<RegionRespVO> regionRespVOs = BeanUtil.copyToList(page.getRecords(), RegionRespVO.class);
        regionRespVOs.forEach(item ->
                item.setSupplierCount(new LambdaQueryChainWrapper<>(supplierMapper)
                        .select(SupplierEntity::getId)
                        .eq(SupplierEntity::getRegionId, item.getId())
                        .count()));
        return new PageResultVO<>(page, regionRespVOs);
    }

}
